// SceneGraph.cpp: implementation of the SceneGraph class.
//
//////////////////////////////////////////////////////////////////////

#include "SceneGraph.h"
#include "SceneObject.h"
#include <vector>
#include <iostream>

SceneGraph::SceneGraph() {}

SceneGraph::~SceneGraph() {}

void SceneGraph::collideScene(SceneObject* caller, SceneObject* called, float timeElapsed)
{
	if (!called) 
	{ 
		std::cerr << "Attempt collide of object not on the scene graph.";
		return;
	}
	else
	{
		called->collide(caller,timeElapsed);
		// and call all the children
		std::vector<SceneObject*>::iterator it = 
                 called->getChildrenObjects()->begin();
		for (; it!=called->getChildrenObjects()->end(); ++it)
		{
			collideScene(caller,*it, timeElapsed);
		}
	}
}

void SceneGraph::updateScene(SceneObject* sceneObject, float timeElapsed)
{
	if (!sceneObject) 
	{ 
		std::cerr << "Attempt update of object not on the scene graph.";
		return;
	}
	else
	{
		sceneObject->update(timeElapsed);
		// and call all the children
		std::vector<SceneObject*>::iterator it = 
                 sceneObject->getChildrenObjects()->begin();
		for (; it!=sceneObject->getChildrenObjects()->end(); ++it)
		{
			updateScene(*it, timeElapsed);
		}
	}
}

void SceneGraph::renderScene(SceneObject* sceneObject, float timeElapsed)
{
	if (!sceneObject) 
	{ 
		std::cerr << "Attempt render of object not on the scene graph.";
		return;
	}
	else
	{
		sceneObject->render(timeElapsed);
		// and call all the children
		std::vector<SceneObject*>::iterator it = 
                 sceneObject->getChildrenObjects()->begin();
		for (; it!=sceneObject->getChildrenObjects()->end(); ++it)
		{
			renderScene(*it, timeElapsed);
		}
		
		sceneObject->postRender(timeElapsed);
	}
}

void SceneGraph::setSceneCamera(SceneObject *sceneObject,SceneObject *camera, float timeElapsed)
{
    std::cout << "Attempt set Scene camera.\n";    
    if (!sceneObject) 
	{ 
		std::cerr << "Attempt set camera of object not on the scene graph.";
		return;
	}
	else
	{
        std::cout << "setting Scene camera.\n";
        std::cout << "am a:"<<sceneObject->getType() <<"\n";
        std::cout <<"you're after:" << RTTI_CAMERA<<".\n";
        if ((sceneObject->getType() == RTTI_CAMERA)&&(sceneObject != camera)){
          std::cout << "found a  camera.\n";
          sceneObject->unsetCamera(timeElapsed);
        }
		// and call all the children
		std::vector<SceneObject*>::iterator it = 
                 sceneObject->getChildrenObjects()->begin();
		for (; it!=sceneObject->getChildrenObjects()->end(); ++it)
		{
			setSceneCamera(*it,camera, timeElapsed);
		}		
	}    
 }
